﻿Imports TransportadoraToledoDAL

Public Class ClienteBLL
    Dim objDAL As ClientesDAL

#Region "propriedades"

    Private codigo As Integer
    Public Property pCodigo() As Integer
        Get
            Return codigo
        End Get
        Set(ByVal value As Integer)
            codigo = value
        End Set
    End Property

    Private codigoCliente As Integer
    Public Property pCodigoCliente() As Integer
        Get
            Return codigoCliente
        End Get
        Set(ByVal value As Integer)
            codigoCliente = value
        End Set
    End Property


    Private codCidade As Integer
    Public Property pCodCidade() As Integer
        Get
            Return Me.codCidade
        End Get
        Set(ByVal value As Integer)
            Me.codCidade = value
        End Set
    End Property




    Private nome As String
    Public Property pNome() As String
        Get
            Return nome
        End Get
        Set(ByVal value As String)
            nome = value
        End Set
    End Property

    Private logradouro As String
    Public Property pLogradouro() As String
        Get
            Return logradouro
        End Get
        Set(ByVal value As String)
            logradouro = value
        End Set
    End Property


    Private complemento As String
    Public Property pComplemento() As String
        Get
            Return complemento
        End Get
        Set(ByVal value As String)
            complemento = value
        End Set
    End Property


    Private numero As String
    Public Property pNumero() As String
        Get
            Return numero
        End Get
        Set(ByVal value As String)
            numero = value
        End Set
    End Property


    Private bairro As String
    Public Property pbairro() As String
        Get
            Return bairro
        End Get
        Set(ByVal value As String)
            bairro = value
        End Set
    End Property


    Private cep As String
    Public Property pcep() As String
        Get
            Return cep
        End Get
        Set(ByVal value As String)
            cep = value
        End Set
    End Property


    Private logradouroc As String
    Public Property plogradouroc() As String
        Get
            Return logradouroc
        End Get
        Set(ByVal value As String)
            logradouroc = value
        End Set
    End Property


    Private complementoc As String
    Public Property pcomplementoc() As String
        Get
            Return complementoc
        End Get
        Set(ByVal value As String)
            complementoc = value
        End Set
    End Property



    Private numeroc As String
    Public Property pnumeroc() As String
        Get
            Return numeroc
        End Get
        Set(ByVal value As String)
            numeroc = value
        End Set
    End Property


    Private bairroc As String
    Public Property pbairroc() As String
        Get
            Return bairroc
        End Get
        Set(ByVal value As String)
            bairroc = value
        End Set
    End Property


    Private cepc As String
    Public Property pcepc() As String
        Get
            Return cepc
        End Get
        Set(ByVal value As String)
            cepc = value
        End Set
    End Property


    Private telefone As String
    Public Property ptelefone() As String
        Get
            Return telefone
        End Get
        Set(ByVal value As String)
            telefone = value
        End Set
    End Property


    Private celular As String
    Public Property pcelular() As String
        Get
            Return celular
        End Get
        Set(ByVal value As String)
            celular = value
        End Set
    End Property


    Private telrecado As String
    Public Property ptelrecado() As String
        Get
            Return telrecado
        End Get
        Set(ByVal value As String)
            telrecado = value
        End Set
    End Property


    Private tipofj As Char
    Public Property ptipofj() As Char
        Get
            Return tipofj
        End Get
        Set(ByVal value As Char)
            tipofj = value
        End Set
    End Property


    Private cpf As String
    Public Property pcpf() As String
        Get
            Return cpf
        End Get
        Set(ByVal value As String)
            cpf = value
        End Set
    End Property


    Private cnpj As String
    Public Property pcnpj() As String
        Get
            Return cnpj
        End Get
        Set(ByVal value As String)
            cnpj = value
        End Set
    End Property


    Private ie As String
    Public Property pie() As String
        Get
            Return ie
        End Get
        Set(ByVal value As String)
            ie = value
        End Set
    End Property


    Private nomefantasia As String
    Public Property pnomefantasia() As String
        Get
            Return nomefantasia
        End Get
        Set(ByVal value As String)
            nomefantasia = value
        End Set
    End Property


    Private ramo As String
    Public Property pramo() As String
        Get
            Return ramo
        End Get
        Set(ByVal value As String)
            ramo = value
        End Set
    End Property


    Private rg As String
    Public Property prg() As String
        Get
            Return rg
        End Get
        Set(ByVal value As String)
            rg = value
        End Set
    End Property


    Private dtnasc As Date
    Public Property pdtnasc() As Date
        Get
            Return dtnasc
        End Get
        Set(ByVal value As Date)
            dtnasc = value
        End Set
    End Property


    Private status As Char
    Public Property pstatus() As Char
        Get
            Return Me.status
        End Get
        Set(ByVal value As Char)
            Me.status = value
        End Set
    End Property

    Private email As Integer
    Public Property pemail() As Integer
        Get
            Return email
        End Get
        Set(ByVal value As Integer)
            email = value
        End Set
    End Property





    Private dadosArray() As String = {"111.111.111-11", "222.222.222-22", "333.333.333-33", "444.444.444-44", _
                                              "555.555.555-55", "666.666.666-66", "777.777.777-77", "888.888.888-88", "999.999.999-99"}
#End Region

#Region "Métodos"
    Sub New()

    End Sub

    Sub New(ByVal codigo As Integer, ByVal codigoCliente As Integer, ByVal nome As String, ByVal logradouro As String, ByVal complemento As String, ByVal numero As String,
                     ByVal bairro As String, ByVal codcidade As Integer, ByVal cep As Char, ByVal logradouroc As String, ByVal complementoc As String, ByVal numeroc As String,
                     ByVal bairroc As String, ByVal cepc As String, ByVal tipofj As Char,
                     ByVal telefone As String, ByVal celular As String, ByVal telrecado As String,
                     ByVal dtnasc As Date, ByVal rg As String, ByVal cpf As String,
                     ByVal nomefantasia As String, ByVal ramo As String, ByVal cnpj As String, ByVal ie As String, ByVal status As Char, email As Integer)
        Me.codigo = codigo
        Me.codigoCliente = codigoCliente
        Me.nome = nome
        Me.logradouro = logradouro
        Me.complemento = complemento
        Me.numero = numero
        Me.bairro = bairro
        Me.codCidade = codcidade
        Me.cep = cep
        Me.logradouroc = logradouroc
        Me.complementoc = complementoc
        Me.numeroc = numeroc
        Me.telefone = telefone
        Me.celular = celular
        Me.telrecado = telrecado
        Me.dtnasc = dtnasc
        Me.rg = rg
        Me.cpf = cpf
        Me.nomefantasia = nomefantasia
        Me.ramo = ramo
        Me.cnpj = cnpj
        Me.ie = ie
        Me.status = status

    End Sub

    Sub inserir()
        Try

            If (nome = "" Or logradouro = "" Or numero = "" Or bairro = "" Or cep = "" Or logradouroc = "" Or numeroc = "" Or bairroc = "" Or cepc = "" Or telefone = "" Or codCidade = 0) Then
                Throw New Exception("Preencha todos os campos!")
            Else
                'inserir
                If cpf <> "" Then

                    objDAL = New ClientesDAL
                    Me.codigo = objDAL.inserir(nome, logradouro, complemento, numero, bairro, codCidade, cep, logradouroc, complementoc, numeroc, bairroc, cepc, tipofj, telefone, celular, telrecado, dtnasc, rg, cpf, nomefantasia, ramo, cnpj, ie, status)


                ElseIf cnpj <> "" Then

                    objDAL = New ClientesDAL
                    Me.codigo = objDAL.inserir(nome, logradouro, complemento, numero, bairro, codCidade, cep, logradouroc, complementoc, numeroc, bairroc, cepc, tipofj, telefone, celular, telrecado, dtnasc, rg, cpf, nomefantasia, ramo, cnpj, ie, status)

                End If

            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub alterar()
        Try
            If (nome = "" Or logradouro = "" Or numero = "" Or bairro = "" Or cep = "" Or logradouroc = "" Or numeroc = "" Or bairroc = "" Or cepc = "" Or telefone = "" Or email = 0) Then
                Throw New Exception("Preencha todos os campos!")
            Else
                'alterar
                objDAL = New ClientesDAL
                objDAL.alterar(codigo, nome, logradouro, complemento, numero, bairro, codCidade, cep, logradouroc, complementoc, numeroc, bairroc, cepc, tipofj, telefone, celular, telrecado, dtnasc, rg, cpf, nomefantasia, ramo, cnpj, ie)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub excluir()
        Try
            If (codigo = 0) Then
                Throw New Exception("Campo(s) inválido(s)!")
            Else
                'excluir
                objDAL = New ClientesDAL
                objDAL.excluir(codigo, tipofj)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub inseriremail(descricao As String, pessoa As Integer)
        Dim objDAL As New ClientesDAL
        Try
            If pessoa = 0 Then
                Throw New Exception("Pessoa não identificada.")
            End If
            If descricao.Trim() = "" Then
                Throw New Exception("Descrição não descrita.")
            End If
            objDAL.inseriremail(descricao, pessoa)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub excluiremail(email As Integer)
        Dim objDAL As New ClientesDAL
        Try
            If email = 0 Then
                Throw New Exception("Usuário não identificado.")
            End If
            objDAL.excluiremail(email)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Function localizaremail(email As Integer) As DataTable
        Dim tab As New DataTable
        Try
            If email = 0 Then
                Throw New Exception("Email não identificado.")
            End If
            objDAL = New ClientesDAL
            tab = objDAL.localizaremail(email)
        Catch ex As Exception
            Throw ex
        End Try
        Return tab
    End Function
    'retorna todas as linhas pesquisadas da tabela
    Function localizar(ByVal tipo As Char, ByVal codigo As Integer, ByVal nome As String) As DataTable
        Dim tab As DataTable = Nothing
        Dim tab2 As DataTable = Nothing
        Try

            objDAL = New ClientesDAL
            tab = objDAL.localizar(tipo, codigo, nome)
            If (tab.Rows.Count > 0) Then
                Me.codigo = tab.Rows(0)("pes_codigo").ToString
                Me.nome = tab.Rows(0)("pes_nome").ToString
                Me.logradouro = tab.Rows(0)("pes_logradouro").ToString
                Me.complemento = tab.Rows(0)("pes_complemento").ToString
                Me.numero = tab.Rows(0)("pes_numero").ToString
                Me.bairro = tab.Rows(0)("pes_bairro").ToString
                Me.bairroc = tab.Rows(0)("pes_bairro_cob").ToString
                Me.cep = tab.Rows(0)("pes_cep").ToString
                Me.cepc = tab.Rows(0)("pes_cep_cob").ToString
                Me.logradouroc = tab.Rows(0)("pes_logradouro").ToString
                Me.complementoc = tab.Rows(0)("pes_complemento_cob").ToString
                Me.numeroc = tab.Rows(0)("pes_numero_cob").ToString
                Me.telefone = tab.Rows(0)("pes_telefone_fixo").ToString
                Me.celular = tab.Rows(0)("pes_celular").ToString
                Me.telrecado = tab.Rows(0)("pes_telefone_recado").ToString
                Me.tipofj = tab.Rows(0)("pes_tipo_fj").ToString
                Me.codCidade = tab.Rows(0)("cod_cidade").ToString
                tab2 = objDAL.localizar(Me.tipofj, Me.codigo, Me.nome)
                If Me.tipofj = "F" Then
                    Me.codigoCliente = tab2.Rows(0)("pf_codigo").ToString
                    Me.dtnasc = tab2.Rows(0)("pf_data_nascimento").ToString
                    Me.rg = tab2.Rows(0)("pf_rg").ToString
                    Me.cpf = tab2.Rows(0)("pf_cpf").ToString
                Else
                    Me.codigoCliente = tab2.Rows(0)("pj_codigo").ToString
                    Me.nomefantasia = tab2.Rows(0)("pj_nome_fantasia").ToString
                    Me.ramo = tab2.Rows(0)("pj_ramo").ToString
                    Me.cnpj = tab2.Rows(0)("pj_cnpj").ToString
                    Me.ie = tab2.Rows(0)("pj_ie").ToString
                End If

            Else
                limpar()
            End If

        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function


    'retorna todas as linhas pesquisadas da tabela
    Function localizarPrimeiroUltimo(ByVal opcao As String, ByVal tipopessoa As Char) As Integer
        Dim tab As DataTable = Nothing
        Dim codigo As Integer = 0
        Dim objDAL As ClientesDAL
        Dim campo As String
        Try
            If (tipopessoa = "F") Then
                campo = "PF_CODIGO"
            Else
                campo = "PJ_CODIGO"
            End If
            If (opcao = "ultimo") Then
                objDAL = New ClientesDAL
                tab = objDAL.localizarPrimeiroUltimo(opcao, tipopessoa)
                If (tab.Rows(0)(campo).ToString <> Nothing) Then
                    codigo = tab.Rows(0)(campo).ToString
                End If
            ElseIf (opcao = "primeiro") Then
                objDAL = New ClientesDAL
                tab = objDAL.localizarPrimeiroUltimo(opcao, tipopessoa)
                If (tab.Rows(0)(campo).ToString <> Nothing) Then
                    codigo = tab.Rows(0)(campo).ToString
                End If
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function


    Function localizarProxAnteriro(ByVal nome As String, ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim CodigoP As Integer = 0
        Try

            objDAL = New ClientesDAL
            tab = objDAL.localizarProxAnterior(Me.tipofj, nome, codigo)
            If (tab.Rows.Count > 0) Then
                If Me.tipofj = "F" Then
                    CodigoP = tab.Rows(0)("pf_codigo").ToString
                ElseIf Me.tipofj = "J" Then
                    CodigoP = tab.Rows(0)("pj_codigo").ToString
                End If
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function

    'localizar uma linha da tabela w preenche as propriedades
    Sub localizarcliente(ByVal codigo As Integer)
        Dim tab As DataTable = Nothing
        Dim tab2 As DataTable = Nothing
        Try
            objDAL = New ClientesDAL
            tab = objDAL.localizarCliente(codigo)

            If (tab.Rows.Count > 0) Then
                Me.codigo = tab.Rows(0)("pes_codigo").ToString
                Me.nome = tab.Rows(0)("pes_nome").ToString
                Me.logradouro = tab.Rows(0)("pes_logradouro").ToString
                Me.complemento = tab.Rows(0)("pes_complemento").ToString
                Me.numero = tab.Rows(0)("pes_numero").ToString
                Me.bairro = tab.Rows(0)("pes_bairro").ToString
                Me.bairroc = tab.Rows(0)("pes_bairro_cob").ToString
                Me.cep = tab.Rows(0)("pes_cep").ToString
                Me.cepc = tab.Rows(0)("pes_cep_cob").ToString
                Me.logradouroc = tab.Rows(0)("pes_logradouro").ToString
                Me.complementoc = tab.Rows(0)("pes_complemento_cob").ToString
                Me.numeroc = tab.Rows(0)("pes_numero_cob").ToString
                Me.telefone = tab.Rows(0)("pes_telefone_fixo").ToString
                Me.celular = tab.Rows(0)("pes_celular").ToString
                Me.telrecado = tab.Rows(0)("pes_telefone_recado").ToString
                Me.tipofj = tab.Rows(0)("pes_tipo_fj").ToString
                tab2 = objDAL.localizar(Me.tipofj, Me.codigo, Me.nome)
                If Me.tipofj = "F" Then
                    Me.dtnasc = tab2.Rows(0)("pf_data_nascimento").ToString
                    Me.rg = tab2.Rows(0)("pf_rg").ToString
                    Me.cpf = tab2.Rows(0)("pf_cpf").ToString
                    Me.codigoCliente = tab2.Rows(0)("pf_codigo").ToString
                Else
                    Me.nomefantasia = tab2.Rows(0)("pj_nome_fantasia").ToString
                    Me.ramo = tab2.Rows(0)("pj_ramo").ToString
                    Me.cnpj = tab2.Rows(0)("pj_cnpj").ToString
                    Me.ie = tab2.Rows(0)("pj_ie").ToString
                    Me.codigoCliente = tab2.Rows(0)("pj_codigo").ToString
                End If




            Else
                limpar()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub localizar(ByVal codigo As Integer)
        Dim tab As DataTable = Nothing
        Dim tab2 As DataTable = Nothing
        Try
            objDAL = New ClientesDAL
            tab = objDAL.localizar(codigo)

            If (tab.Rows.Count > 0) Then
                Me.codigo = tab.Rows(0)("pes_codigo").ToString
                Me.nome = tab.Rows(0)("pes_nome").ToString
                Me.logradouro = tab.Rows(0)("pes_logradouro").ToString
                Me.complemento = tab.Rows(0)("pes_complemento").ToString
                Me.numero = tab.Rows(0)("pes_numero").ToString
                Me.bairro = tab.Rows(0)("pes_bairro").ToString
                Me.bairroc = tab.Rows(0)("pes_bairro_cob").ToString
                Me.cep = tab.Rows(0)("pes_cep").ToString
                Me.cepc = tab.Rows(0)("pes_cep_cob").ToString
                Me.logradouroc = tab.Rows(0)("pes_logradouro").ToString
                Me.complementoc = tab.Rows(0)("pes_complemento_cob").ToString
                Me.numeroc = tab.Rows(0)("pes_numero_cob").ToString
                Me.telefone = tab.Rows(0)("pes_telefone_fixo").ToString
                Me.celular = tab.Rows(0)("pes_celular").ToString
                Me.telrecado = tab.Rows(0)("pes_telefone_recado").ToString
                Me.tipofj = tab.Rows(0)("pes_tipo_fj").ToString
                tab2 = objDAL.localizar(Me.tipofj, Me.codigo, Me.nome)
                If Me.tipofj = "F" Then
                    Me.dtnasc = tab2.Rows(0)("pf_data_nascimento").ToString
                    Me.rg = tab2.Rows(0)("pf_rg").ToString
                    Me.cpf = tab2.Rows(0)("pf_cpf").ToString
                Else
                    Me.nomefantasia = tab2.Rows(0)("pj_nome_fantasia").ToString
                    Me.ramo = tab2.Rows(0)("pj_ramo").ToString
                    Me.cnpj = tab2.Rows(0)("pj_cnpj").ToString
                    Me.ie = tab2.Rows(0)("pj_ie").ToString
                End If




            Else
                limpar()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub localizar(ByVal codigo As Integer, ByVal nome As String)
        Dim tab As DataTable = Nothing
        Dim tab2 As DataTable = Nothing
        Dim tab3 As DataTable = Nothing

        Try
            objDAL = New ClientesDAL
            tab = objDAL.localizar(tipofj, codigo, nome)

            If (tab.Rows.Count > 0) Then
                Me.codigo = tab.Rows(0)("pes_codigo").ToString
                Me.nome = tab.Rows(0)("pes_nome").ToString
                Me.logradouro = tab.Rows(0)("pes_logradouro").ToString
                Me.complemento = tab.Rows(0)("pes_complemento").ToString
                Me.numero = tab.Rows(0)("pes_numero").ToString
                Me.bairro = tab.Rows(0)("pes_bairro").ToString
                Me.bairroc = tab.Rows(0)("pes_bairro_cob").ToString
                Me.cep = tab.Rows(0)("pes_cep").ToString
                Me.cepc = tab.Rows(0)("pes_cep_cob").ToString
                Me.logradouroc = tab.Rows(0)("pes_logradouro").ToString
                Me.complementoc = tab.Rows(0)("pes_complemento_cob").ToString
                Me.numeroc = tab.Rows(0)("pes_numero_cob").ToString
                Me.telefone = tab.Rows(0)("pes_telefone_fixo").ToString
                Me.celular = tab.Rows(0)("pes_celular").ToString
                Me.telrecado = tab.Rows(0)("pes_telefone_recado").ToString
                Me.tipofj = tab.Rows(0)("pes_tipo_fj").ToString


                tab2 = objDAL.localizar(Me.tipofj, Me.codigo, Me.nome)
                If Me.tipofj = "F" Then
                    Me.dtnasc = tab2.Rows(0)("pf_data_nascimento").ToString
                    Me.rg = tab2.Rows(0)("pf_rg").ToString
                    Me.cpf = tab2.Rows(0)("pf_cpf").ToString
                    Me.codigoCliente = tab2.Rows(0)("pf_codigo").ToString
                Else
                    Me.nomefantasia = tab2.Rows(0)("pj_nome_fantasia").ToString
                    Me.ramo = tab2.Rows(0)("pj_ramo").ToString
                    Me.cnpj = tab2.Rows(0)("pj_cnpj").ToString
                    Me.ie = tab2.Rows(0)("pj_ie").ToString
                    Me.codigoCliente = tab2.Rows(0)("pj_codigo").ToString
                End If




            Else
                limpar()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Function localizar(ByVal nome As String) As DataTable
        Dim tab As DataTable = Nothing
        Try

            objDAL = New ClientesDAL
            tab = objDAL.localizar(nome)

        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function

    Sub limpar()
        Me.codigo = Nothing
        Me.nome = Nothing
        Me.codigoCliente = Nothing
        Me.logradouro = Nothing
        Me.complemento = Nothing
        Me.numero = Nothing
        Me.bairro = Nothing
        Me.codCidade = Nothing
        Me.cep = Nothing
        Me.logradouroc = Nothing
        Me.complementoc = Nothing
        Me.numeroc = Nothing
        Me.telefone = Nothing
        Me.celular = Nothing
        Me.telrecado = Nothing
        Me.dtnasc = Nothing
        Me.rg = Nothing
        Me.cpf = Nothing
        Me.nomefantasia = Nothing
        Me.ramo = Nothing
        Me.cnpj = Nothing
        Me.ie = Nothing
        Me.status = Nothing
        Me.email = Nothing
    End Sub

    Function ValidaCPF(ByVal CPF As String) As Boolean

        Dim i, x, n1, n2 As Integer

        CPF = CPF.Trim
        For i = 0 To dadosArray.Length - 1
            If CPF.Length <> 14 Or dadosArray(i).Equals(CPF) Then
                Return False
            End If
        Next
        'remove a maskara
        CPF = CPF.Substring(0, 3) + CPF.Substring(4, 3) + CPF.Substring(8, 3) + CPF.Substring(12)
        For x = 0 To 1
            n1 = 0
            For i = 0 To 8 + x
                n1 = n1 + Val(CPF.Substring(i, 1)) * (10 + x - i)
            Next
            n2 = 11 - (n1 - (Int(n1 / 11) * 11))
            If n2 = 10 Or n2 = 11 Then n2 = 0

            If n2 <> Val(CPF.Substring(9 + x, 1)) Then
                Return False
            End If
        Next

        Return True
    End Function

    Function ValidaCNPJ(ByVal CNPJ As String) As Boolean

        Dim i As Integer
        Dim valida As Boolean
        CNPJ = CNPJ.Trim

        For i = 0 To dadosArray.Length - 1
            If CNPJ.Length <> 18 Or dadosArray(i).Equals(CNPJ) Then
                Return False
            End If
        Next

        'remove a maskara
        CNPJ = CNPJ.Substring(0, 2) + CNPJ.Substring(3, 3) + CNPJ.Substring(7, 3) + CNPJ.Substring(11, 4) + CNPJ.Substring(16)
        valida = efetivaValidacao(CNPJ)

        If valida Then
            ValidaCNPJ = True
        Else
            ValidaCNPJ = False
        End If

    End Function

    Function efetivaValidacao(ByVal cnpj As String)
        Dim Numero(13) As Integer
        Dim soma As Integer
        Dim i As Integer
        'Dim valida As Boolean
        Dim resultado1 As Integer
        Dim resultado2 As Integer
        For i = 0 To Numero.Length - 1
            Numero(i) = CInt(cnpj.Substring(i, 1))
        Next
        soma = Numero(0) * 5 + Numero(1) * 4 + Numero(2) * 3 + Numero(3) * 2 + Numero(4) * 9 + Numero(5) * 8 + Numero(6) * 7 + _
                   Numero(7) * 6 + Numero(8) * 5 + Numero(9) * 4 + Numero(10) * 3 + Numero(11) * 2
        soma = soma - (11 * (Int(soma / 11)))
        If soma = 0 Or soma = 1 Then
            resultado1 = 0
        Else
            resultado1 = 11 - soma
        End If

        If resultado1 = Numero(12) Then
            soma = Numero(0) * 6 + Numero(1) * 5 + Numero(2) * 4 + Numero(3) * 3 + Numero(4) * 2 + Numero(5) * 9 + Numero(6) * 8 + _
                         Numero(7) * 7 + Numero(8) * 6 + Numero(9) * 5 + Numero(10) * 4 + Numero(11) * 3 + Numero(12) * 2
            soma = soma - (11 * (Int(soma / 11)))
            If soma = 0 Or soma = 1 Then
                resultado2 = 0
            Else
                resultado2 = 11 - soma
            End If
            If resultado2 = Numero(13) Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If

    End Function


#End Region
End Class
